package listener.notifications;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.widget.AbsListView;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;
import controller.notifcations.CompanyNotificationsAsync;
import ui.activity.login.Login;

/**
 * Created by Dong Le on 11/3/2014.
 */
public class CompanyNotificationsScrollListener implements AbsListView.OnScrollListener {

    public static int visibleThreshold = 5;
    public static int currentPage = 0;
    public static int previousTotal = 0;
    public static boolean loading = true;
    private Context mContext;

    public CompanyNotificationsScrollListener(Context context, int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
        this.mContext = context;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {

                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {

            // I load the next page of gigs using a background task,
            // but you can call any function here.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                new CompanyNotificationsAsync(mContext).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                        String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                        Login.user.getUserInfo().getUserName(),
                        String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                        mContext.getResources().getString(R.string.true_),
                        currentPage + "",
                        mContext.getResources().getString(R.string.enterprise_company)
                );
            else
                new CompanyNotificationsAsync(mContext).execute(
                        String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                        Login.user.getUserInfo().getUserName(),
                        String.valueOf(Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                        mContext.getResources().getString(R.string.true_),
                        currentPage + "",
                        mContext.getResources().getString(R.string.enterprise_company)
                );
            loading = true;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
}